<script>
    $(function  () {
      layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(login)', function(data){
          $('#sub').hide();
          $.ajax({
            url: "{*login_api*}",
            type: "POST",
            data: data.field,
            success: function (res) {
              if (res.code != 0) {
                return layer.msg(res.msg, {time:2000}, function () {
                  $('#sub').show();
                });
              }
              layer.msg(res.msg, {time:1000}, function () {
                  window.location.href = res.url + '?token=' + res.token
              });
            },
            error: function (res) {
              return layer.msg("请求失败", {time:2000}, function () {
                $('#sub').show();
              });
            }
          })
          return false;
        });
      });
    })
</script>
